package edu.ncsu.csc.itrust.dao.officevisit;

import java.sql.SQLException;
import edu.ncsu.csc.itrust.beans.LabProcedureBean;
import edu.ncsu.csc.itrust.dao.mysql.LabProcedureDAO;
import edu.ncsu.csc.itrust.dao.mysql.OfficeVisitLabProcedureDAO;
import edu.ncsu.csc.itrust.datagenerators.TestDataGenerator;
import edu.ncsu.csc.itrust.exception.DBException;
import edu.ncsu.csc.itrust.testutils.EvilDAOFactory;
import edu.ncsu.csc.itrust.testutils.TestDAOFactory;
import junit.framework.TestCase;

public class OVLabProcedureTest extends TestCase{

	private OfficeVisitLabProcedureDAO ovlpDAO = TestDAOFactory.getTestInstance().getOVLabProcedureDAO();
	private OfficeVisitLabProcedureDAO evillpDAO = EvilDAOFactory.getEvilInstance().getOVLabProcedureDAO();
	private LabProcedureDAO lpDAO = TestDAOFactory.getTestInstance().getLabProcedureDAO();
	
	/**
	 * Set up the database with the info
	 */
	public void setUp() throws Exception{
		TestDataGenerator gen = new TestDataGenerator();
		gen.clearAllTables();
		gen.officeVisit1();
		gen.labProcedures();
	}
	
	/**
	 * SQL Fails
	 */
	public void testSQLFails() throws Exception{
		setUp();
		
		long lpID = 0;
		try{
			lpID= evillpDAO.addLabProcedureToOfficeVisit("10763-1", 1, 1);
			fail();
		}catch(DBException e){
			//should get to here
		}
		LabProcedureBean lproc = new LabProcedureBean();
		lproc.setProcedureID(lpID);
		lproc.setOvID(1);
		lproc.setLoinc("10666-6");
		try{
			evillpDAO.editLabProcedure(lproc);
			fail();
		}catch(DBException e){
			//should get here
		}
		try{
			evillpDAO.removeLabProcedureFromOfficeVisit(lpID);
			fail();
		}catch(DBException e){
			//should get here
		}
	}
	
    /**
	 * Test adding, editing, and removing a lab procedure from an office visit
	 * @throws Exception
	 */
	public void testAddEditRemoveLabProcedureToOfficeVisit() throws Exception{
		//Add
		long ovlID = ovlpDAO.addLabProcedureToOfficeVisit("10763-1", 1, 1);
		LabProcedureBean temp = lpDAO.getLabProcedure(ovlID);
		assertEquals(1, temp.getOvID());
		assertEquals("10763-1", temp.getLoinc());
		//Edit
		LabProcedureBean lproc = new LabProcedureBean();
		lproc.setLoinc("10666-6");
		lproc.setOvID(1);
		lproc.setProcedureID(ovlID);
		ovlID = ovlpDAO.editLabProcedure(lproc);
		temp = lpDAO.getLabProcedure(ovlID);
		assertEquals(1, temp.getOvID());
		assertEquals("10666-6", temp.getLoinc());
		//Remove
		ovlpDAO.removeLabProcedureFromOfficeVisit(ovlID);
		try{
			temp = lpDAO.getLabProcedure(ovlID);
			fail();	//should not complete
		}catch(DBException e){
			//we did it!
		}		
	}
	
}
